package com.esri.android.map;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;

/* loaded from: classes.dex */
public class GroupLayer extends Layer {
    final List<Layer> a;
    private long b;

    native int nativeAddLayer(long j, long j2);

    native boolean nativeCheckSpatialReference(long j, int i, String str);

    native void nativeClear(long j);

    native long nativeCreateAddGraphicLayerRequestCallback();

    native long nativeCreateLayer();

    native double[] nativeGetGroupLayerFullExtent(long j);

    native long nativeGetLayerByIndex(long j, int i);

    native long nativeGetLayerByName(long j, String str);

    native boolean nativeInsertLayer(long j, long j2, int i);

    native int nativeLayerCount(long j);

    native boolean nativeQueryLayerByIndex(long j, long j2, int i);

    native boolean nativeQueryLayerByName(long j, long j2, String str);

    native void nativeRemoveLayer(long j, int i);

    public GroupLayer() {
        this(true);
    }

    public GroupLayer(boolean z) {
        this.a = Collections.synchronizedList(new ArrayList());
        this.b = -1L;
        if (z) {
            try {
                getServiceExecutor().submit(new Runnable() { // from class: com.esri.android.map.GroupLayer.1
                    @Override // java.lang.Runnable
                    public void run() {
                        GroupLayer.this.initLayer();
                    }
                });
            } catch (RejectedExecutionException e) {
                e.printStackTrace();
            }
        }
    }

    @Override // com.esri.android.map.Layer
    protected long create() {
        return nativeCreateLayer();
    }

    @Override // com.esri.android.map.Layer
    protected void initLayer() {
        this.b = nativeCreateAddGraphicLayerRequestCallback();
    }

    @Override // com.esri.android.map.Layer
    public boolean isInitialized() {
        if (this.a.size() > 0) {
            return getLayer(0).isInitialized();
        }
        return false;
    }

    public Layer[] getLayers() {
        Layer[] layerArr;
        synchronized (this.a) {
            layerArr = (Layer[]) this.a.toArray(new Layer[0]);
        }
        return layerArr;
    }

    public Layer[] getLayers(String str) {
        if (this.nativeHandle == 0 || str == null || str.length() <= 0) {
            return new Layer[0];
        }
        ArrayList arrayList = new ArrayList();
        a(arrayList, str);
        return (Layer[]) arrayList.toArray(new Layer[0]);
    }

    private void a(List<Layer> list, String str) {
        for (Layer layer : this.a) {
            if (layer.getName() != null && layer.getName().equals(str)) {
                list.add(layer);
            }
            if (layer instanceof GroupLayer) {
                ((GroupLayer) layer).a(list, str);
            }
        }
    }

    public Layer getLayerByID(long j) {
        Layer layer = null;
        for (Layer layer2 : this.a) {
            if (layer2.getID() == j) {
                return layer2;
            }
            if ((layer2 instanceof GroupLayer) && (layer = ((GroupLayer) layer2).getLayerByID(j)) != null) {
                return layer;
            }
        }
        return layer;
    }

    public Layer getLayer(int i) {
        Layer layer;
        synchronized (this.a) {
            layer = i < this.a.size() ? this.a.get(i) : null;
        }
        return layer;
    }

    public int addLayer(Layer layer) {
        int nativeAddLayer;
        if (this.nativeHandle == 0 || layer == null) {
            return -1;
        }
        synchronized (this.a) {
            if (!a(this, layer)) {
                nativeAddLayer = nativeAddLayer(this.nativeHandle, layer.getID());
                this.a.add(layer);
                a(layer);
            } else {
                throw new IllegalArgumentException("The layer oject is null, or index is out of range, or the layer has been existed.");
            }
        }
        return nativeAddLayer;
    }

    private void a(Layer layer) {
        if (this.n != null && this.m != null && layer.n == null && layer.m == null) {
            layer.n = this.n;
            layer.m = this.m;
            if (layer instanceof GroupLayer) {
                GroupLayer groupLayer = (GroupLayer) layer;
                if (groupLayer.getLayers() != null && groupLayer.getLayers().length > 0) {
                    for (Layer layer2 : groupLayer.getLayers()) {
                        a(layer2);
                    }
                }
            }
            if (layer instanceof TiledLayer) {
                a(layer, TiledLayer.a);
            } else if (layer instanceof DynamicLayer) {
                a(layer, DynamicLayer.a);
            }
        }
        layer.q = this;
    }

    private void a(Layer layer, float f) {
        float p = this.m.p();
        if (layer.isRenderNativeResolution()) {
            layer.setRenderPixelsPerInch(p);
        } else if (p >= f) {
            layer.setRenderPixelsPerInch(f);
        } else {
            layer.setRenderPixelsPerInch(p);
        }
    }

    public boolean addLayer(Layer layer, int i) {
        if (this.nativeHandle == 0 || layer == null) {
            return false;
        }
        synchronized (this.a) {
            if (!a(this, layer)) {
                if (nativeInsertLayer(this.nativeHandle, layer.nativeHandle, i)) {
                    if (i <= this.a.size()) {
                        this.a.add(i, layer);
                    } else {
                        this.a.add(layer);
                    }
                    a(layer);
                    return true;
                }
                return false;
            }
            throw new IllegalArgumentException("The layer oject is null,or index is out of range, or the layer has been existed.");
        }
    }

    private boolean a(GroupLayer groupLayer, Layer layer) {
        for (int i = 0; i < groupLayer.count(); i++) {
            Layer layer2 = groupLayer.getLayer(i);
            if (layer2.nativeHandle == layer.nativeHandle) {
                return true;
            }
            if (layer2 instanceof GroupLayer) {
                return a((GroupLayer) layer2, layer);
            }
        }
        return false;
    }

    public void addLayers(Layer[] layerArr) {
        if (this.nativeHandle == 0 || layerArr == null || layerArr.length <= 0) {
            return;
        }
        synchronized (this.a) {
            for (Layer layer : layerArr) {
                if (layer != null && this.nativeHandle != 0 && !a(this, layer)) {
                    nativeAddLayer(this.nativeHandle, layer.getID());
                    this.a.add(layer);
                    a(layer);
                } else {
                    throw new IllegalArgumentException("The layer oject is null,or index is out of range, or the layer has been existed.");
                }
            }
        }
    }

    public void removeAll() {
        if (this.nativeHandle != 0) {
            nativeClear(this.nativeHandle);
        }
    }

    public boolean hasLayer(String str) {
        if (this.nativeHandle == 0 || str == null) {
            return false;
        }
        synchronized (this.a) {
            for (Layer layer : this.a) {
                if (layer.getName() != null && layer.getName().equalsIgnoreCase(str)) {
                    return nativeQueryLayerByName(this.nativeHandle, layer.nativeHandle, str);
                }
            }
            return false;
        }
    }

    public boolean hasLayer(int i) {
        if (this.nativeHandle == 0 || this.a.size() <= 0 || i >= this.a.size()) {
            return false;
        }
        synchronized (this.a) {
            Layer layer = this.a.get(i);
            if (layer != null) {
                return nativeQueryLayerByIndex(this.nativeHandle, layer.nativeHandle, i);
            }
            return false;
        }
    }

    public void removeLayer(int i) {
        if (this.nativeHandle == 0 || this.a.size() <= 0 || i >= this.a.size()) {
            return;
        }
        synchronized (this.a) {
            Layer layer = this.a.get(i);
            if (layer != null) {
                nativeRemoveLayer(this.nativeHandle, i);
                this.a.remove(i);
                layer.n = null;
                layer.m = null;
                layer.q = null;
            }
        }
    }

    public void removeLayers(String str) {
        if (this.nativeHandle == 0 || this.a.size() <= 0 || str == null || str.length() <= 0) {
            return;
        }
        synchronized (this.a) {
            a(this, str);
        }
    }

    private void a(GroupLayer groupLayer, String str) {
        int indexOf;
        Iterator<Layer> it = groupLayer.a.iterator();
        while (it.hasNext()) {
            Layer next = it.next();
            if (next.getName() != null && next.getName().equalsIgnoreCase(str) && (indexOf = this.a.indexOf(next)) >= 0) {
                nativeRemoveLayer(this.nativeHandle, indexOf);
                it.remove();
                next.n = null;
                next.m = null;
                next.q = null;
            }
            if (next instanceof GroupLayer) {
                a((GroupLayer) next, str);
            }
        }
    }

    public int count() {
        if (this.nativeHandle != 0) {
            return nativeLayerCount(this.nativeHandle);
        }
        return 0;
    }

    @Override // com.esri.android.map.Layer
    public Envelope getFullExtent() {
        double[] nativeGetGroupLayerFullExtent;
        if (this.nativeHandle == 0 || (nativeGetGroupLayerFullExtent = nativeGetGroupLayerFullExtent(this.nativeHandle)) == null || nativeGetGroupLayerFullExtent.length != 4) {
            return null;
        }
        return new Envelope(nativeGetGroupLayerFullExtent[0], nativeGetGroupLayerFullExtent[1], nativeGetGroupLayerFullExtent[2], nativeGetGroupLayerFullExtent[3]);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public void setDefaultSpatialReference(SpatialReference spatialReference) {
        super.setDefaultSpatialReference(spatialReference);
    }

    @Override // com.esri.android.map.Layer
    public SpatialReference getDefaultSpatialReference() {
        if (super.getDefaultSpatialReference() != null) {
            return super.getDefaultSpatialReference();
        }
        if (this.a.size() <= 0 || !this.a.get(0).isInitialized()) {
            return null;
        }
        return this.a.get(0).getDefaultSpatialReference();
    }

    @Override // com.esri.android.map.Layer
    public void recycle() {
        c();
        super.recycle();
    }

    private void c() {
        try {
            synchronized (this.a) {
                for (Layer layer : this.a) {
                    layer.recycle();
                }
            }
        } finally {
            this.a.clear();
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public void finalize() throws Throwable {
        c();
        super.finalize();
    }

    @Override // com.esri.android.map.Layer
    public void setOpacity(float f) {
        if (this.nativeHandle != 0) {
            nativeSetOpacity(this.nativeHandle, f);
            this.i = f;
        }
    }

    public long getAddGraphicsLayerRequestCallbackHandle() {
        if (this.b == -1) {
            this.b = nativeCreateAddGraphicLayerRequestCallback();
        }
        return this.b;
    }

    private void a(GroupLayer groupLayer, float f) {
        for (int i = 0; i < groupLayer.count(); i++) {
            Layer layer = groupLayer.getLayer(i);
            layer.setOpacity(layer.getOpacity() * f);
            if (layer instanceof GroupLayer) {
                a((GroupLayer) layer, f);
            }
        }
    }

    protected void graphicsLayerAddRequest(String str, int i) throws Exception {
        GraphicsLayer graphicsLayer = new GraphicsLayer();
        graphicsLayer.setName(str);
        if (i >= this.a.size()) {
            addLayer(graphicsLayer);
        } else {
            addLayer(graphicsLayer, i);
        }
    }
}
